package com.neuedu.system.service.impl;
import com.neuedu.system.domain.LandResources;
import com.neuedu.system.domain.LivestockNumbers;
import com.neuedu.system.mapper.LandResourcesMapper;
import com.neuedu.system.mapper.LivestockNumbersMapper;
import lombok.RequiredArgsConstructor;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.neuedu.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.neuedu.system.domain.CropPlantingAreas;
import com.neuedu.system.mapper.AgriculturalReportsMapper;
import com.neuedu.system.domain.AgriculturalReports;
import com.neuedu.system.service.IAgriculturalReportsService;

/**
 * 农业报告基本信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-17
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AgriculturalReportsServiceImpl extends ServiceImpl<AgriculturalReportsMapper, AgriculturalReports> implements IAgriculturalReportsService
{
    private final AgriculturalReportsMapper agriculturalReportsMapper;

    /**
     * 查询农业报告基本信息
     *
     * @param id 农业报告基本信息主键
     * @return 农业报告基本信息
     */
    @Override
    public AgriculturalReports selectAgriculturalReportsById(Long id)
    {
        return agriculturalReportsMapper.selectAgriculturalReportsById(id);
    }

    /**
     * 查询农业报告基本信息列表
     *
     * @param agriculturalReports 农业报告基本信息
     * @return 农业报告基本信息
     */
    @Override
    public List<AgriculturalReports> selectAgriculturalReportsList(AgriculturalReports agriculturalReports)
    {
        return agriculturalReportsMapper.selectAgriculturalReportsList(agriculturalReports);
    }

    /**
     * 新增农业报告基本信息
     *
     * @param agriculturalReports 农业报告基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAgriculturalReports(AgriculturalReports agriculturalReports)
    {
        int rows = agriculturalReportsMapper.insertAgriculturalReports(agriculturalReports);
        insertCropPlantingAreas(agriculturalReports);
        return rows;
    }

    /**
     * 修改农业报告基本信息
     *
     * @param agriculturalReports 农业报告基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAgriculturalReports(AgriculturalReports agriculturalReports)
    {
        agriculturalReportsMapper.deleteCropPlantingAreasByReportId(agriculturalReports.getId());
        agriculturalReportsMapper.deleteLandResourcesByReportId(agriculturalReports.getId());
        agriculturalReportsMapper.deleteLivestockNumbersByReportId(agriculturalReports.getId());

        insertCropPlantingAreas(agriculturalReports);
        return agriculturalReportsMapper.updateAgriculturalReports(agriculturalReports);
    }

    /**
     * 批量删除农业报告基本信息
     *
     * @param ids 需要删除的农业报告基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAgriculturalReportsByIds(Long[] ids)
    {
        agriculturalReportsMapper.deleteCropPlantingAreasByReportIds(ids);
        return agriculturalReportsMapper.deleteAgriculturalReportsByIds(ids);
    }

    /**
     * 删除农业报告基本信息信息
     *
     * @param id 农业报告基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAgriculturalReportsById(Long id)
    {
        agriculturalReportsMapper.deleteCropPlantingAreasByReportId(id);
        return agriculturalReportsMapper.deleteAgriculturalReportsById(id);
    }

    @Autowired
    private LivestockNumbersMapper livestockNumbersMapper;

    @Autowired
    private LandResourcesMapper landResourcesMapper;
    /**
     * 新增农作物种植面积信息、畜牧业养殖数量表、土地资源利用情况表
     *
     * @param agriculturalReports 农业报告基本信息对象
     */
    public void insertCropPlantingAreas(AgriculturalReports agriculturalReports)
    {
        List<LandResources> landResourcesList = agriculturalReports.getLandResourcesList();
        for (LandResources landResources : landResourcesList) {
            landResources.setReportId(agriculturalReports.getId());
            landResourcesMapper.insertLandResources(landResources);
        }

        List<LivestockNumbers> livestockNumbersList = agriculturalReports.getLivestockNumbersList();

        for (LivestockNumbers livestockNumbers : livestockNumbersList) {
            livestockNumbers.setReportId(agriculturalReports.getId());
            livestockNumbersMapper.insertLivestockNumbers(livestockNumbers);
        }

        List<CropPlantingAreas> cropPlantingAreasList = agriculturalReports.getCropPlantingAreasList();
        Long id = agriculturalReports.getId();
        if (StringUtils.isNotNull(cropPlantingAreasList)){
            List<CropPlantingAreas> list = new ArrayList<CropPlantingAreas>();
            for (CropPlantingAreas cropPlantingAreas : cropPlantingAreasList){
                cropPlantingAreas.setReportId(id);
                list.add(cropPlantingAreas);
            }
            if (list.size() > 0){
                agriculturalReportsMapper.batchCropPlantingAreas(list);
            }
        }
    }
}
